<template>
  <div class="monitor-schedule-table">
    <tag-content-header search refresh setting @toolFun="toolFun" :settingData="columns">
      <template #title>
        <span class="title-text">调度任务明细</span>
      </template>
    </tag-content-header>

    <tag-table
      ref="table"
      @refresh="getList"
      :data="dataList"
      :loading="loading"
      :columns="columns"
      :params="params"
      :total="totalSize"
    >
      <template #operation="{ scope }">
        <div class="operation-button-box">
          <el-tooltip
            v-if="scope.row.status === 0 || scope.row.status === 3"
            effect="dark"
            content="任务挂起"
            placement="top"
            :enterable="false"
          >
            <icon-svg name="iconPause" @click.native="handlePause(scope.row)"></icon-svg>
          </el-tooltip>
          <el-tooltip v-if="scope.row.status === 1" effect="dark" content="任务唤醒" placement="top" :enterable="false">
            <icon-svg name="iconResume" @click.native="handleResume(scope.row)"></icon-svg>
          </el-tooltip>
          <el-tooltip
            v-if="scope.row.status === 0 || scope.row.status === 3"
            effect="dark"
            content="任务重跑"
            placement="top"
            :enterable="false"
          >
            <icon-svg name="iconRedo" @click.native="handleRedo(scope.row)"></icon-svg>
          </el-tooltip>
          <el-tooltip effect="dark" content="查看工作流历史" placement="top" :enterable="false">
            <icon-svg name="iconLog" @click.native="handleFlow(scope.row)"></icon-svg>
          </el-tooltip>
        </div>
      </template>
    </tag-table>

    <flow-dialog ref="flowDialog"></flow-dialog>
  </div>
</template>

<script>
import flowDialog from './schedule-flow-history'
export default {
  components: {
    flowDialog
  },
  data() {
    return {
      dataList: [],
      loading: false,
      columns: [
        {
          show: true,
          label: '业务模块',
          prop: 'execType',
          dict: '$exec_module_type',
          search: true,
          searchKey: '',
          sort: true,
          align: 'center'
        },
        {
          show: true,
          label: '任务名称',
          prop: 'execTaskNm',
          search: true,
          searchKey: '',
          sort: true,
          align: 'center'
        },
        {
          show: true,
          label: '调度序号',
          prop: 'execId',
          search: true,
          searchKey: '',
          sort: true,
          align: 'center'
        },
        {
          show: true,
          label: '预计下次开始时间',
          prop: 'nextExecTm',
          search: true,
          searchKey: '',
          searchType: 'dateTime',
          width: 180,
          align: 'center'
        },
        {
          show: true,
          label: '任务状态',
          prop: 'status',
          dict: '$exec_task_code',
          search: true,
          searchKey: '',
          width: 100,
          dot: {
            0: '#45b972',
            1: '#205694',
            2: '#4daac9',
            3: '#f96363'
          },
          align: 'center'
        },
        {
          show: true,
          label: '创建时间',
          prop: 'createTm',
          width: 180,
          align: 'center'
        },
        {
          show: 'fixed',
          label: '操作',
          prop: 'operation',
          align: 'center',
          width: 160
        }
      ],
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0
    }
  },
  methods: {
    getList() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('exec/execScheduleJob/scheduleTaskPage'),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          if (data.data.currentPageNum === this.params.currentPageNum) {
            this.dataList = data.data.records
            this.totalSize = data.data.totalSize
          }
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    toolFun(operation) {
      switch (operation) {
        case 'search':
          // 搜索动作
          this.$refs['table'].searchToggle()
          break
        case 'refresh':
          // 刷新动作
          this.$emit('refresh')
          this.getList()
          break
      }
    },
    handlePause(row) {
      this.$confirm('确定要挂起该任务吗？')
        .then(() => {
          let messageBox = document.getElementsByClassName('el-message-box__wrapper')
          messageBox[0].focus() // 防止按空格键重复确定
          this.$ajax({
            url: this.$ajax.setUrl(`exec/execScheduleJob/pauseJob/${row.execId}`),
            method: 'post',
            data: this.$ajax.setData()
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.$message.success(data.msg)
              this.$emit('refresh')
              this.getList()
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        })
        .catch(() => {})
    },
    handleResume(row) {
      this.$confirm('确定要唤醒该任务吗？')
        .then(() => {
          let messageBox = document.getElementsByClassName('el-message-box__wrapper')
          messageBox[0].focus() // 防止按空格键重复确定
          this.$ajax({
            url: this.$ajax.setUrl(`exec/execScheduleJob/resumeJob/${row.execId}`),
            method: 'post',
            data: this.$ajax.setData()
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.$message.success(data.msg)
              this.$emit('refresh')
              this.getList()
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        })
        .catch(() => {})
    },
    handleRedo(row) {
      this.$confirm('确定要重跑该任务吗？')
        .then(() => {
          let messageBox = document.getElementsByClassName('el-message-box__wrapper')
          messageBox[0].focus() // 防止按空格键重复确定
          this.$ajax({
            url: this.$ajax.setUrl(`exec/execScheduleJob/executorJob/${row.execId}`),
            method: 'post',
            data: this.$ajax.setData()
          }).then(({ data }) => {
            if (data && data.code === '0') {
              this.$message.success(data.msg)
              this.$emit('refresh')
              this.getList()
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        })
        .catch(() => {})
    },
    handleFlow(row) {
      this.$refs.flowDialog.showDialog(row)
    }
  }
}
</script>

<style lang="scss"></style>
